package com.park.web.controller;

import com.park.web.entity.User;
import com.park.web.result.Result;
import com.park.web.result.ResultFactory;
import com.park.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


//import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

@Api(description = "用户接口")
@Controller
@RequestMapping(value = "/api/v1/users")

//@RestController//进行根url路径的映射
public class HomeController {

    @Autowired //由框架自动进行实例化
    UserService userService;

    @ApiOperation(value = "用户登录" ,  notes="用户登录")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Result login(@RequestBody User requestUser, HttpSession session) {
        String username = requestUser.getUsername();
        username = HtmlUtils.htmlEscape(username);

        User user = userService.get(username, requestUser.getPassword());
        if (null == user) {
            String message = "账号密码错误";
            return ResultFactory.buildFailResult(message);
        } else {
            session.setAttribute("user", user);
            return ResultFactory.buildSuccessResult(user);
        }
    }

    @ApiOperation(value = "新增用户" ,  notes="新增用户")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestBody User requestUser) {
        String username = requestUser.getUsername();
        username = HtmlUtils.htmlEscape(username);

        boolean isExist = userService.isExist(username);
        if (!isExist) {
            userService.add(requestUser);
            return ResultFactory.buildSuccessResult("添加成功");
        } else {
            return ResultFactory.buildFailResult("用户已存在");
        }
    }

    @ApiOperation(value = "删除用户" ,  notes="删除用户")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody User requestUser) {
        int result = userService.delete(requestUser.getId());
        return result > 0 ? ResultFactory.buildSuccessResult("删除成功") : ResultFactory.buildFailResult("删除失败");
    }

    @ApiOperation(value = "更新用户" ,  notes="更新用户")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody User requestUser) {
        int result = userService.update(requestUser);
        return result > 0 ? ResultFactory.buildSuccessResult("更新成功") : ResultFactory.buildFailResult("更新失败");
    }

//    @RequestMapping("/")
//    public String Index(){
//        return "hello";
//
//    }
//    @RequestMapping("/SetSession")
//    public String SetSession(HttpServletRequest request){
//        HttpSession session=request.getSession();//这就是session的创建
//        session.setAttribute("username","TOM");//给session添加属性属性name： username,属性 value：TOM
//        session.setAttribute("password","tommmm");//添加属性 name: password; value: tommmm
//        return "写入Session成功";
//    }
//
//    @RequestMapping("/GetSession")
//    public String GetSession(HttpServletRequest request){
//        HttpSession session=request.getSession();//这就是session的创建
//        String test= session.getAttribute("username").toString();
//        session.getAttribute("password");
//        return test;
//    }
}
